<?php

namespace App\Common;

class DataConvert
{
    /**
     * Notes:加密数据
     * User: pace
     * Date: 2021/01/18
     * @param $value
     * @return string
     */
    public static function encode($value): string
    {
        $data = match (true) {
            is_array($value) => [
                'value_type' => 'array',
                'value' => json_encode($value, JSON_UNESCAPED_UNICODE)
            ],
            is_object($value) => [
                'value_type' => 'object',
                'value' => json_encode($value)
            ],
            default => [
                'value_type' => 'default',
                'value' => $value
            ],
        };

        return json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    /**
     * Notes:解密数据
     * @param mixed $data
     * @return mixed
     */
    public static function decode(string $data): mixed
    {
        $data = json_decode($data,true);
        return match ($data['value_type']) {
            'array' => json_decode($data['value'], true),
            'object' => json_decode($data['value']),
            default => $data['value'],
        };
    }
}